const {Service} = require('egg');

class BlogService extends Service{
    async blogList(){
        const bloglist = await this.app.mysql.query('select * from bloglist');
        //数据库的交互
        return bloglist
    }
    //发表博客
    async add(title,con,uid){
        let time = new Date();
        await this.app.mysql.query('insert into bloglist (title,con,uid,create_time) values (?,?,?,?)',[title,con,uid,time])
    }
    //删除博客
    async del(id,uid){
        return await this.app.mysql.query('delete from bloglist where id=? and uid=?',[id,uid])
    }
    //编辑博客
    async edit(title,con,uid,id){
        let time = new Date();
        await this.app.mysql.query('update bloglist set title=?,con=?,uid=?,create_time=? where id=?',[title,con,uid,time,id])
    }
    //查询
    async bloglist(){
        let list = await this.app.mysql.query('select * from bloglist');

        for(let i = 0;i<list.length;i++){
            let user = await this.app.mysql.query('select username from userlist where id=?',[list[i].uid])
            list[i].username = user[0].username;
        }
        return list

    }
    
}

module.exports = BlogService